package MovieGuessingGame.confirmRegistration;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JFrame;

// import MovieGuessingGame.player.GameJFrame;

public class ConfirmRegistrationController {

	private RegistrationClientSSL clientConnect = null;
	ConfirmRegistrationJPanel confReg = null;
	JFrame frame = null;
	
	private void init(String host)
	{
		clientConnect = new RegistrationClientSSL();
		// clientConnect.clientConnect(host);
	}
	void displayPanel()
	{
		confReg = new ConfirmRegistrationJPanel();
		frame = new JFrame();
		frame.getContentPane().add(confReg);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		confReg.btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//Validate data entered...
				String validData = validateDataEntered(confReg.getUserName(), confReg.getRegCodeField(), confReg.getPassword());
				
				if (!"".equals(validData)) {
					confReg.setMessageFieldText(validData);
					frame.repaint();
					frame.setVisible(false);
					frame.setVisible(true);
					frame.setSize(500, 400);
					confReg.setVisible(true);
				}
				else {
					clientConnect.clientConnect("localhost");
					setUpConnectionToServer(confReg.getUserName(), confReg.getRegCodeField(), confReg.getPassword());
					System.out.println("HERE WE ARE");
				}
			}
		});
		frame.addWindowListener( 
			    new WindowAdapter() { 
			        public void windowClosing(WindowEvent e) { 
			            //System.exit(0); 
			        } 
			    } 
			);
		frame.setSize(500, 400);
		confReg.setVisible(true);
		frame.setVisible(true);
		
	}
	
	private String validateDataEntered(String userName, String regCode, String password) {
		String retVal = "";
		
		Pattern p1 = Pattern.compile("^[a-zA-Z0-9]+$");
		Matcher m1 = p1.matcher(userName);
		
		if (!m1.matches()) {
			return "Invalid username format";
		}
		
		Pattern p2 = Pattern.compile("^[0-9]+$");
		Matcher m2 = p2.matcher(regCode);
		
		if (!m2.matches()) {
			return "Invalid registration code";
		}
		
		Matcher m3 = p1.matcher(password);
		
		if (!m3.matches()) {
			return "Invalid password format!";
		}
		
		return retVal;
	}
	
	private void setUpConnectionToServer(String userName, String regCode, String password)
	{
		System.out.println("The values are: " + userName + " " + regCode + " " + password);
		try {
			clientConnect.clientSend(userName,regCode,password);
			int outcome = clientConnect.clientReceive();
			if (outcome == -1)
			{
				confReg.setMessageFieldText(clientConnect.getMessage());
				frame.repaint();
				frame.setVisible(false);
				frame.setVisible(true);
				frame.setSize(500, 400);
				confReg.setVisible(true);
			}
			else{
				confReg.setMessageFieldText(clientConnect.getMessage());
				frame.repaint();
				frame.setVisible(false);
				frame.setVisible(true);
				frame.setSize(500, 400);
				confReg.setVisible(true);				
				// frame.dispose();
				//while (true){
				//Thread t = new Thread(new GameJFrame("localhost",clientConnect.hashValue));
		        //t.start();
		        //System.out.println("Waiting");
		        //InputStreamReader converter = new InputStreamReader(System.in);
		        //BufferedReader in = new BufferedReader(converter);
		        //in.readLine();
				//}
		        
				//GameJFrame frame = new GameJFrame("localhost");
				//frame.run();
//				frame.setVisible(true);
//				frame.setResizable(false);
//				frame.play();
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ConfirmRegistrationController confRegControl = new ConfirmRegistrationController();
		confRegControl.init("localhost");
		confRegControl.displayPanel();
	}
}
